import { defineStore } from 'pinia'
import { getUserInfo } from '@api/user'
import { Role } from '@interface/user'

export const useInfoStore = defineStore('userId', {
  state: (): {
    roles: Role[] //数组类型为什么这么写？
    rolePerm: string
  } => {
    return {
      roles: [],
      rolePerm: ''
    }
  },
  getters: {},
  actions: {
    //获取用户信息
    async getUserInfo() {
      let res = await getUserInfo()
      //解构赋值
      const { permissions, roles, units, userInfo } = res.data
      this.roles = roles
      this.rolePerm = roles[0].rolePerm
    }
  },
  persist: {
    enabled: true, //开启数据存储
    strategies: [
      {
        storage: localStorage, //默认走session
        paths: ['rolePerm'] //rolePerm持久化存储
      }
    ]
  }
})
